package com.jxtc.enterprise.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jxtc.enterprise.common.entity.Store;
import com.jxtc.enterprise.common.mapper.StoreMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class MerchantStoreManageService {

    private final StoreMapper storeMapper;

    /**
     * 通过店主手机号查询店铺 ID
     */
    public String queryStoreIdByMerchantPhone(String phone) {
        LambdaQueryWrapper<Store> queryWrapper = new LambdaQueryWrapper<Store>()
                .eq(Store::getMerchantPhone, phone)
                .eq(Store::getDelFlag, false)
                .orderByDesc(Store::getCreateTime);
        List<Store> storeList = storeMapper.selectList(queryWrapper);
        if (storeList.isEmpty()) {
            log.error("queryStoreIdByMerchantPhone, can't find store by phone, phone: {}", phone);
            throw new IllegalArgumentException("商户手机号为 " + phone + " 的店铺不存在，请联系管理员排查");
        }
        // 如果该手机号绑定了多个店铺，则取创建时间最新的店铺的 ID
        return storeList.get(0).getId();
    }

}
